package xyz.wadewhy.basicstest.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * fastJson比较常用，但是会有些坑，比如字段不匹配，直接把值丢弃
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
class Student {
    /**
     * ordinal 序列化顺序
     */
    @JSONField(name = "Name", ordinal = 1)
    private String name;
    @JSONField(name = "Age")
    private Integer age;
    /**
     * 不序列化
     */
    @JSONField(serialize = false)
    private String nikeName;
    @JSONField(name = "Books")
    private List<Book> books;
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class Book {
    private String desc;
    private Double price;
    /**
     * format 格式
     */
    @JSONField(format = "yyyy-MM-dd HH:mm:ss.SSS")
    private Date time;
}

/**
 * 对象工具类
 */
class ModelUtil {
    public static List<Student> buildStudents() {
        List<Student> students = new ArrayList<Student>();
        Book book1 = new Book("java编程", 22.9, new Date());
        Book book2 = new Book("C编程", 12.9, new Date());
        Book book3 = new Book("C++编程", 202.9, new Date());
        Book book4 = new Book("kotlin编程", 298.9, new Date());
        students.add(new Student("wadewhy", 24, "wadewhy", new ArrayList<Book>() {
            {
                this.add(book1);
                this.add(book2);
            }
        }));
        students.add(new Student("LBJ", 37, "lbj", new ArrayList<Book>() {
            {
                this.add(book1);
                this.add(book2);
                this.add(book3);
            }
        }));
        students.add(new Student("Bill", 37, "bill", new ArrayList<Book>() {
            {
                this.add(book2);
                this.add(book3);
            }
        }));
        students.add(new Student("kill", 37, "Kiss", new ArrayList<Book>() {
            {
                this.add(book1);
                this.add(book2);
                this.add(book3);
                this.add(book4);
            }
        }));
        return students;
    }
}

class TestDemoV1 {
    public static void main(String[] args) {
//        pojoToJson();
        jsonToPojo();
    }

    /**
     * 对象转json
     */
    public static void pojoToJson() {
        List<Student> students = ModelUtil.buildStudents();
        String json = JSON.toJSONString(students);
        System.err.println(json);
    }

    /**
     * json转对象
     * 注意反序列化时为对象时，必须要有默认无参的构造函数，否则会报异常
     */
    public static void jsonToPojo() {
        String json = "[{\"Age\":24,\"books\":[{\"desc\":\"java编程\",\"price\":22.9},{\"desc\":\"C编程\",\"price\":12.9}],\"name\":\"wadewhy\"},{\"age\":37,\"books\":[{\"$ref\":\"$[0].books[0]\"},{\"$ref\":\"$[0].books[1]\"},{\"desc\":\"C++编程\",\"price\":202.9}],\"name\":\"LBJ\"},{\"age\":37,\"books\":[{\"$ref\":\"$[0].books[1]\"},{\"$ref\":\"$[1].books[2]\"}],\"name\":\"Bill\"},{\"age\":37,\"books\":[{\"$ref\":\"$[0].books[0]\"},{\"$ref\":\"$[0].books[1]\"},{\"$ref\":\"$[1].books[2]\"},{\"desc\":\"kotlin编程\",\"price\":298.9}],\"name\":\"Bill\"}]";
        List<Student> students = JSON.parseArray(json, Student.class);
        for (Student it : students
        ) {
            System.out.println(it.toString());
        }
    }
}

